package com.wz.demo.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangze
 * @date 2018-03-01 14:47.
 **/
@Controller

public class LoginController {

    @RequestMapping("login")
    String index() {
        /**
         * 已经登录过了
         */
        if (SecurityUtils.getSubject().isAuthenticated()) {
            return "redirect:/home/index";
        }
        return "login/login";
    }


    @RequestMapping(value = "login/submit", method = RequestMethod.POST)
    @ResponseBody
    Map<String, Object> login(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);

        try {
            AuthenticationToken token = new UsernamePasswordToken(username, password);
            SecurityUtils.getSubject().login(token);
            return map;
        } catch (Exception ex) {
            map.replace("success", false);
            map.put("msg", ex.getMessage());
            return map;
        }

    }


//    @RequestMapping("/login/submit")
//    String submit(String userName, String pwd) {
//
//        try {
//            AuthenticationToken token = new UsernamePasswordToken(userName, pwd);
//            new Subject.Builder().buildSubject().login(token);
//
//        } catch (Exception ex) {
//
//        }
//
//
//    }


    @RequestMapping("logout")
    String logout() {
        return "";
    }


    @RequestMapping("main/index")
    String main() {
        return "login/main";
    }


}
